using System;
using System.Collections;
using DailyReport.Core.Bussines.IManagers;
using DailyReport.Core.Data.Dao;
using DailyReport.Core.Data.Dao.DaoFactory;
using DailyReport.Core.Data.Model;
using DailyReport.Core.Util;
using log4net;

namespace DailyReport.Core.Bussines
{
    public class DailyManager : IDailyManager
    {
        private ILog log = LogManager.GetLogger("DailyReportManager");
        private IDailyDao dailyDao;
        private ITeamRegisterDao registerDao;

        public DailyManager()
        {
            log.Debug("Creating manager");
            dailyDao = (IDailyDao) DaoFactory.i.getDaoInstance("DailyDao");
            log.Debug("Session:" + ConnectionManager.Instance);
            dailyDao.Session = ConnectionManager.Instance.Session;

            registerDao = (ITeamRegisterDao) DaoFactory.i.getDaoInstance("TimeRegisterDao");
            registerDao.Session = ConnectionManager.Instance.Session;
        }

        public void CreateTimeRegister(TimeRegister register, int userId)
        {
            Daily currentDaily;
            TransactionManager transactionManager = ConnectionManager.Instance.BeginTransaction();
            TimeRegister currentRegister = registerDao.getCurrentActiveRegister(userId);
            if (currentRegister != null)
            {
                currentRegister.EndTime = DateTime.Now;              
                currentDaily = currentRegister.Daily;
                registerDao.SaveOrUpdate(currentRegister);
            }
            else
                currentDaily = dailyDao.GetCurrentDailyByUser(userId);

            register.Daily = currentDaily;
            register.EndTime = null;
            log.Debug("CreateTimeRegister....");
            registerDao.Save(register);
            transactionManager.Commit();
        }

        public void UpdateTimeRegister(TimeRegister register, int userId)
        {
            registerDao.SaveOrUpdate(register);
        }

        public void StartDaily(int userId)
        {
            Daily daily = new Daily();
            daily.User = new User();
            daily.User.ID = userId;
            daily.Current = true;
            daily.StartTime = DateTime.Now;
            daily.EndTime = new DateTime(1754, 1, 1);
            dailyDao.Save(daily);
        }

        public void EndDaily(int userId)
        {
            log.Debug("User:" + userId);
            TransactionManager transactionManager = ConnectionManager.Instance.BeginTransaction();
            Daily daily = GetDailyByUser(userId);
            daily.Current = false;
            daily.EndTime = DateTime.Now;
            log.Debug("EndDaily..." + daily);
            dailyDao.SaveOrUpdate(daily);
            transactionManager.Commit();
        }

        public Daily GetDailyByUser(int userId)
        {
            return dailyDao.GetCurrentDailyByUser(userId);
        }

        //[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        //public IList GetDailyRegistersList(int userId, string sortExpression, int startRowIndex, int maxRows)
        public IList GetDailyRegistersList(int userId, string sortExpression)
        {
            log.Debug("GetDailyRegistersList");
            return registerDao.getTimeRegistersList(userId, sortExpression);
        }

        public int GetDailyRegistersCount(int userId)
        {
            return registerDao.getTimeRegisterCount(userId);
        }

        public IList GetInstanceList(string sortExpression, int startRowIndex, int maxRows)
        {
            return registerDao.getBaseInstancesList(sortExpression, maxRows, startRowIndex);
        }

        public int CountInstances()
        {
            return registerDao.getBaseInstancesCount();
        }
    }
}